package com.liuqing.www.web过滤器;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.game.common.AllConstants;
import com.game.common.DESPlus;
import com.game.common.UserContext;
import com.game.common.WmlTools;
import com.game.user.OnlineUserInfo;

public class URLFilter implements Filter {

	protected FilterConfig config;

	public void destroy() {

	}
	public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
//		System.out.println("[SpecialCharacterFilter] : doFilter *************" );
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		HashMap<String, String[]> m = new HashMap(request.getParameterMap());
		HashMap<String, String[]> mm = new HashMap<String, String[]>();
		Enumeration<String> enu = request.getParameterNames();
		if (m.containsKey(AllConstants.SSID) && m.size() > 0 && processParameters(m, enu, mm)) {
			ParameterRequestWrapper wrapRequest = new ParameterRequestWrapper(
					request, mm);
			chain.doFilter(wrapRequest, res);
		} else {
			chain.doFilter(req, res);
		}
	}

	public void init(FilterConfig arg0) throws ServletException {
		this.config = arg0;
	}

	public boolean processParameters(HashMap<String, String[]> m, Enumeration enu, HashMap<String, String[]> mm) {  
//         System.out.println("[SpecialCharacterFilter] : processParameters *************");
         
    	 if (m != null && enu != null) {
             while (enu.hasMoreElements()) {
                 String key = (String)enu.nextElement();
                 String [] values = (String [])m.get(key);
                 String value = values[0] ;
      			 DESPlus des = new DESPlus("leemenz");
      			 String urlParams = des.decrypt(value) ;
      			 String[] urlParam = urlParams.split("&amp;") ;
      			 for(String urlp : urlParam){
      				 if("".equals(urlp)){
      					 continue ;
      				 }
      				 mm.put(urlp.substring(0,urlp.indexOf("=")), new String[]{urlp.substring(urlp.indexOf("=")+1 , urlp.length())});
      			 }
             }
    	 }
         return true;  
   
     }
	public boolean checkAccess(HttpServletRequest req,HttpServletResponse res,HashMap<String, String[]> m)throws IOException, ServletException{
		if(m.containsKey("sessionid")&&m.get("sessionid")!=null&&m.get("sessionid").length>0){
			String sessionid=m.get("sessionid")[0];
			OnlineUserInfo user=UserContext.get(sessionid);
			if(req.getRequestURI().indexOf("answer.jsp")>-1||
					req.getRequestURI().indexOf("game!answorQuestion")>-1||
					req.getRequestURI().indexOf("game%21answorQuestion")>-1||
					req.getRequestURI().indexOf("exit.action")>-1
		
					){
				return false;
			}
			if(user==null||user.getFAvo().getLastPageContent()==null){
				return false;
			}
			if(user.getOperation()==1){//GM
				return false;
			}
			user.getFAvo().checkIsTimeToAnswer();
			if(user.getFAvo().checkCanAnswer()){
				String path  = (String)req.getContextPath() ;
				res.sendRedirect(path+"/user/answer.jsp?sessionid="+sessionid);
				return true;
			}
			if(AllConstants.checkAccessTime(user)){
				user.getFAvo().setLastPageCount(user.getFAvo().getLastPageCount()+1);
				res.setContentType("text/vnd.wap.wml");
				res.getWriter().print(user.getFAvo().getLastPageContent());
				res.getWriter().flush();
				res.getWriter().close();
				return true;
			}
		}
		return false;
	}
}
